package com.siwei.mes.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.siwei.mes.common.query.PageQuery;
import com.siwei.mes.entity.ExperimentImage;
import com.siwei.mes.mapper.ExperimentImageMapper;
import com.siwei.mes.service.ExperimentImageService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

import static com.siwei.mes.common.exception.handler.MessageHandler.assertTrue;

/**
 * 试验台账图片(ExperimentImage)表服务实现类
 *
 * @author linzi
 * @since 2024-01-01 16:37:33
 */
@Service
public class ExperimentImageServiceImpl implements ExperimentImageService {
    @Resource
    private ExperimentImageMapper experimentImageMapper;

    @Override
    public Long create(ExperimentImage entity) {
        experimentImageMapper.insertSelective(entity);
        return entity.getId();
    }

    @Override
    public void update(ExperimentImage entity) {
        ExperimentImage experimentImage = experimentImageMapper.selectByPrimaryKey(entity.getId());
        assertTrue(experimentImage == null, "试验台账图片信息不存在");
        experimentImageMapper.updateByPrimaryKeySelective(entity);
    }

    @Override
    public void delete(Long id) {
        ExperimentImage experimentImage = experimentImageMapper.selectByPrimaryKey(id);
        assertTrue(experimentImage == null, "试验台账图片信息不存在");
        experimentImageMapper.deleteByPrimaryKey(id);
    }

    @Override
    public PageInfo<ExperimentImage> getPage(PageQuery<ExperimentImage, ExperimentImage> query) {
        ExperimentImage searchParam = query.getParams();
        return PageHelper.startPage(query.getPageNum(), query.getPageSize())
                .doSelectPageInfo(() -> experimentImageMapper.getList(searchParam));
    }

    @Override
    public ExperimentImage loadById(Long id) {
        return experimentImageMapper.loadById(id);
    }

    @Override
    public List<ExperimentImage> getAll(ExperimentImage entity) {
        return experimentImageMapper.getList(entity);
    }

    @Override
    public List<ExperimentImage> selectByExperimentId(Long experimentId) {
        return this.getAll(new ExperimentImage().setExperimentId(experimentId));
    }
}
